package com.ctakit.sdk.app.base;

import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.ctakit.sdk.SdkApplication;
import com.ctakit.sdk.app.util.ViewUtil;
import com.ctakit.sdk.app.widget.fragmentmaster.app.MasterFragment;
import com.ctakit.sdk.util.TUtil;
import com.ctakit.sdk.view.ViewFinder;
import com.ctakit.sdk.view.ViewInjector;
import com.kaopiz.kprogresshud.KProgressHUD;

import java.io.Serializable;
import java.util.Map;

/**
 * <p>
 * Created by andyhome on 16/05/20.
 */
public abstract class BaseFragment<T extends BasePresenter, E extends BaseModel> extends MasterFragment implements MyActivity {

    protected T mPresenter;

    protected E mModel;

    protected KProgressHUD progressDialog;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = ViewInjector.inject(this, inflater, container);
        mPresenter = TUtil.getT(this, 0);
        mModel = TUtil.getT(this, 1);
        if (this instanceof BaseView) {
            mPresenter.setVM(this, mModel);
        }
        return view;


    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ViewFinder viewFinder = new ViewFinder(view);
        ViewInjector.injectFields(this, viewFinder);
        ViewInjector.injectMethod(this, viewFinder);
    }

    @Override
    public View findView(int id) {
        if (getFragment() != null && getFragment().getView() != null) {
            return getFragment().getView().findViewById(id);
        } else {
            return null;
        }
    }

    ////////start/////////代码 和 baseActivity 基本一样，只是没有多继承，只好复制一份，修改时记得两边都修改

    @Override
    public KProgressHUD getProgressDialog() {
        if (progressDialog == null) {
            progressDialog = KProgressHUD.create(getActivity())
                    .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                    .setLabel("加载中...")
                    .setCancellable(true)
                    .setAnimationSpeed(2);
        } else {
            progressDialog.dismiss();
        }

        return progressDialog;
    }

    @Override
    public void showProgressDialog() {
        getProgressDialog().setLabel("加载中...").show();
    }

    @Override
    public void showProgressDialog(final String msg) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (getActivity() != null) {
                    try {
                        getProgressDialog().setLabel(msg).show();
                    } catch (NullPointerException e) {

                    }
                }
            }
        }, 0);


    }

    @Override
    public void cancelProgressDialog() {

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (getProgressDialog().isShowing()) {
                    getProgressDialog().dismiss();
                }
            }
        }, 0);

    }

    @Override
    public void showToastMsg(String msg) {
        // ToastHelper.makeText(getActivity(), msg, ToastHelper.LENGTH_SHORT).setAnimation(R.style.popToast).show();
        Toast toast = Toast.makeText(SdkApplication.app().getApplicationContext(), msg, Toast.LENGTH_SHORT);
        if (toast != null) {
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (goBack(null, true)) {
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void alert(String msg) {

    }


    public boolean goBack(View v, boolean isPhoneBack) {
        return false;
    }

    ///////// activity 跳转

    /**
     * 跳转到下一个 activity
     * * Map<String, Serializable> data = new HashMap<String, Serializable>();
     * data.put("mobile", mobile);
     */

    public void gotoActivity(Class clazz, Map<String, Serializable> data) {
        gotoActivity(clazz, data, null);
    }

    public void gotoActivity(Class clazz, Map<String, Serializable> data, Map<String, Parcelable> parcelableMap) {
        ViewUtil.gotoActivity(this, clazz, data, parcelableMap);
    }

    // 跳转到下一个 activity(不传数据)

    /**
     * @param clazz
     */
    public void gotoActivity(Class clazz) {
        gotoActivity(clazz, null);
    }

    // 跳转到下一个 activity
    public void gotoActivityForResult(Class<?> clazz, Map<String, Serializable> data) {
        ViewUtil.gotoActivityForResult(this.getActivity(), clazz, data);
    }

    // 跳转到下一个 activity
    public void bottomToActivity(Class<?> clazz, Map<String, Serializable> data) {
        ViewUtil.bottomToActivity(this.getActivity(), clazz, data);
    }

    ///////////////end///////////////////


    public abstract void gotoFragment(Class clazz);

    public abstract void gotoFragment(Class clazz, Map<String, Serializable> data);

    public abstract void gotoFragment(Class clazz, Map<String, Serializable> data, Map<String, Parcelable> parcelableMap);


    public BaseFragment getFragment() {
        return this;
    }

    ;


    public abstract void startFragmentForResult(Class clazz, int requestCode, Map<String, Serializable> data);


    public abstract void startFragmentForResult(Class clazz, int requestCode, Map<String, Serializable> data, Map<String, Parcelable> parcelableMap);

    @Override
    public MyActivity getMyActivity() {
        return getFragment();
    }

}


